﻿namespace WriterStars.Models
{
    using System.Security.Principal;
    
    using Microsoft.AspNet.Identity;

    using WriterStars.Data;
    using WriterStars.Model;

    public static class Extensions
    {
        public static string GetFirstName(this IIdentity identity)
        {
            var currentUser = Extensions.CurrentUser(identity);
            if (currentUser != null)
            {
                return currentUser.FirstName;
            }
            else
            {
                return string.Empty;
            }
        }

        public static string GetFullName(this IIdentity identity)
        {
            var currentUser = Extensions.CurrentUser(identity);
            if(currentUser != null)
            {
                return currentUser.FirstName + " " + currentUser.LastName;
            }
            else
            {
                return string.Empty;
            }
        }

        private static User CurrentUser(IIdentity identity)
        {
            var db = new WriterStarsDbContext();

            var currentUser = db.Users.Find(identity.GetUserId());

            return currentUser;
        }
    }
}